import com.mxgraph.util.mxConstants;
import com.mxgraph.view.mxGraph;
import javax.swing.JSlider;
//==============================================================================
public class Dijkstra {
  private int i,j,k,y=26,t=0,xr,yr,l=0;
  private int MAX_VERTS;
  private int d[];
  private int inf;
  private boolean used[];
  //============================================================================
  public void Init(ReadFileClass file,mxGraph graph,Object parent,mxGraph twograph,Object parent2) throws InterruptedException{
   t=0;l=0;
   MAX_VERTS=file.GetMaxVertex();
    inf = 1000000000;
    used = new boolean[MAX_VERTS];
    d = new int[MAX_VERTS];
    for(i=0;i<MAX_VERTS;i++){
        d[i]=inf;used[i]=false;
        float theta = (float) (360/MAX_VERTS  * Math.PI * i / 180.0f);        
        graph.insertVertex(parent,null,i+1, 350+15*MAX_VERTS * Math.cos(theta),250+15*MAX_VERTS * Math.sin(theta), 24, 24,"shape=ellipse;");
        graph.setCellStyles(mxConstants.STYLE_FONTSIZE, "18",new Object[]{graph.getModel().getChildAt(parent,i)});
        graph.setCellStyles(mxConstants.STYLE_FONTCOLOR, "white",new Object[]{graph.getModel().getChildAt(parent,i)});
        graph.setCellStyles(mxConstants.STYLE_FILLCOLOR, "black",new Object[]{graph.getModel().getChildAt(parent,i)});
    }
    xr=26;yr=26;
    for(i=0;i<MAX_VERTS+1;i++){        
        if(i==0){
            twograph.insertVertex(parent2,null,"",-16+xr, -16+yr, 24, 24,"shape=label;");
            xr+=26;t++;l++;
        } else {
            twograph.insertVertex(parent2,null,i,-16+xr, -16+yr, 24, 24,"shape=label;");
            xr+=26;t++;l++;
        }
    }
    xr=26;yr=52;
    for(i=0;i<MAX_VERTS;i++){        
            twograph.insertVertex(parent2,null,(i+1)+"шаг",-16+xr, -16+yr, 24, 24,"shape=label;");
            yr+=26;t++;
    }
    yr=52;xr=52;
    for(i=0;i<MAX_VERTS;i++){
         for(j=0;j<MAX_VERTS;j++){
             if(file.matr[i][j]!=0){ 
                 twograph.insertVertex(parent2,null,"inf",-16+xr, -16+yr, 24, 24,"shape=label;");
             } else {
                 if(i==j){
                    twograph.insertVertex(parent2,null,"inf",-16+xr, -16+yr, 24, 24,"shape=label;");
                 } else {
                     twograph.insertVertex(parent2,null,"inf",-16+xr, -16+yr, 24, 24,"shape=label;");
                 }
             }
             xr+=26;
             }
         yr+=26;xr=52;
         }
    for(i=0;i<MAX_VERTS;i++){
         for(j=0;j<MAX_VERTS;j++){
             if(file.matr[i][j]!=0){ 
                  graph.insertEdge(parent,null,file.matr[i][j],graph.getModel().getChildAt(parent,i),graph.getModel().getChildAt(parent,j));
                  graph.setCellStyles(mxConstants.STYLE_STROKECOLOR, "black",graph.getEdgesBetween(graph.getModel().getChildAt(parent,i),graph.getModel().getChildAt(parent,j)));     
                  graph.setCellStyles(mxConstants.STYLE_FONTSIZE, "15",graph.getEdgesBetween(graph.getModel().getChildAt(parent,i),graph.getModel().getChildAt(parent,j)));
                  graph.setCellStyles(mxConstants.STYLE_FONTCOLOR, "red",graph.getEdgesBetween(graph.getModel().getChildAt(parent,i),graph.getModel().getChildAt(parent,j)));
             }
         }
    }
  }
  //============================================================================
  public boolean Dijkstra(ReadFileClass file,mxGraph graph,Object parent,mxGraph twograph,Object parent2,int mychoicevert,JSlider slider) throws InterruptedException{
     int curr=t,c=0;
     int min=inf,chvert=-1;
     int[] minvalue = new int [MAX_VERTS];
     boolean[] minvalueused = new boolean [MAX_VERTS];
     for(i=0;i<MAX_VERTS;i++){
       d[i]=inf;used[i]=false;minvalue[i]=inf;minvalueused[i]=false;
     }
     d[mychoicevert-1]=0;
     twograph.setCellStyles(mxConstants.STYLE_FILLCOLOR, "pink",new Object[]{twograph.getModel().getChildAt(parent2,mychoicevert)});
     int from,zfrom;
     while (true){
         from=-1;zfrom=inf;
         for(i=0;i<MAX_VERTS;i++)
             if ((zfrom>d[i]) && !(used[i])) {
                 from=i;zfrom=d[i];
             }
             if (zfrom>=inf) break;
             used[from]=true;
             graph.setCellStyles(mxConstants.STYLE_FILLCOLOR, "blue", new Object[]{graph.getModel().getChildAt(parent,from)});
             Thread.sleep(slider.getValue());
         for(int to=0;to<MAX_VERTS;to++){
             if(file.matr[from][to]!=0){
                 Thread.sleep(slider.getValue());
                 graph.setCellStyles(mxConstants.STYLE_STROKECOLOR, "blue",graph.getEdgesBetween(graph.getModel().getChildAt(parent,from),graph.getModel().getChildAt(parent,to)));
                 Thread.sleep(slider.getValue());
                 graph.setCellStyles(mxConstants.STYLE_STROKECOLOR, "black",graph.getEdgesBetween(graph.getModel().getChildAt(parent,from),graph.getModel().getChildAt(parent,to)));
                 if ((!used[to]) && (d[to]>d[from]+file.matr[from][to])) {
                     d[to]=d[from]+file.matr[from][to];
                     minvalue[to] = curr;
                     twograph.getModel().setValue(twograph.getModel().getChildAt(parent2,curr), d[to]);
                     curr++;
                 } else { 
                     twograph.getModel().setValue(twograph.getModel().getChildAt(parent2,curr), d[to]); 
                     curr++; 
                 }
             } else {
                 if(to==mychoicevert-1){
                  twograph.getModel().setValue(twograph.getModel().getChildAt(parent2,curr), 0); 
                 } 
                 if(d[to]!=inf){
                     twograph.getModel().setValue(twograph.getModel().getChildAt(parent2,curr), d[to]);
                 }
                 curr++;
             }
         }
         min = inf;
         int val = 0;
         for(int b = 0;b<d.length;b++){
             if(min>d[b] && !minvalueused[b] && d[b]!=0){
                 min = d[b];
                 chvert = minvalue[b]+(MAX_VERTS*c);
                 val = b;
             }
         }
         if(chvert!=-1){
             minvalueused[val] = true;
             c++;
             Thread.sleep(slider.getValue());
             twograph.setCellStyles(mxConstants.STYLE_FILLCOLOR, "pink",new Object[]{twograph.getModel().getChildAt(parent2,chvert)});
         }
     }
     return false;
   }
   //===========================================================================
   public int retmaxverts(){
       return MAX_VERTS;
   }
}